﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml.Serialization;
using System.IO;
using Emico.Linq.Criterion;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;

namespace Emico.Core.Tests.Criterion
{
    [TestClass]
    public class CriteriaTest
    {
        [TestMethod]
        public void TestSerialize()
        {
            string propertyName = "Name";

            LambdaExpression<TestType, string> e = new LambdaExpression<TestType, string>((t, A0) => t.Name == A0, propertyName);
            OrderBy<TestType>[] o = new OrderBy<TestType>[]
            {
                new OrderBy<TestType>(t => t.Name)
            };

            Criteria<TestType> criteria = new Criteria<TestType>(e, o);
            using(MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(ms,criteria);

                ms.Position = 0;
                var result = (Criteria<TestType>)bf.Deserialize(ms);

                Assert.AreEqual(1, result.Orders.Count);
                Assert.IsTrue(result.Filter is LambdaExpression<TestType>);
            }
        }

        [TestMethod]
        public void TestEmptyConstructor()
        {
            Criteria<TestType> criteria = new Criteria<TestType>();
            using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(ms, criteria);

                ms.Position = 0;
                var result = (Criteria<TestType>)bf.Deserialize(ms);

                Assert.IsTrue(result.Filter is EmptyExpression<TestType>);
            }
        }
    }
}
